# controller

# responsible for the build of the region-screen
# puts the html-pieces together

from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from google.appengine.ext.db import djangoforms

from Utilities import *
from model.octopus.datastore.OctopusCloudDatabase import *
from getpass import *

class RegionForm(djangoforms.ModelForm):
    class Meta:
        model = OctopusCloudDatabase
        # type in the names of the attributes, which have to be ignored..       
        exclude = ['user', 'endpointURL', 'port', 'credentialType']
        
class BuildRegion(webapp.RequestHandler):    
    # put the region-information on screen
    # pSelf is the self-attribute from the caller-class
    def get(self, pSelf, templateValues, credentials):
        utilities = Utilities()

        credentialsAmount = credentials.count()
        results = credentials.fetch(100)
        
        # menu-text
        credentialsText = utilities.getMenuTextCredentials()
        yourDataText = utilities.getMenuTextYourData()
        infoText = utilities.getMenuTextInfo()
        
        # get other place-holder
        title = utilities.getTitleText()
        userText = templateValues['userText']
        username = templateValues['user']
        url = templateValues['url']
        urlText = templateValues['urlText']
        hintText = templateValues['hintText']
        hintColor = templateValues['hintColor']
        
        headline = self.buildHeadline()
        loginTable = self.buildLoginTable(credentialsAmount, results)
        formText = self.buildFormText()
        loginForm = self.buildLoginForm()
        content = self.buildContent()
        
        finalTemplateValues = {
                               'title': title,
                               'userText': userText,
                               'user': username,
                               'url': url,
                               'urlText': urlText,
                               'hintText': hintText,
                               'hintColor': hintColor,
                               'credentials': credentialsText,
                               'yourData': yourDataText,
                               'info': infoText,
                               'headline': headline,
                               'loginTable': loginTable,
                               'formText': formText,
                               'loginForm': loginForm,
                               'content': content
                               }
        
        html = template.render('view/templates/regions.html', finalTemplateValues)
        
        pSelf.response.out.write(html)

        
    # responsible for the formular-data-input    
    def post(self):
        formularFields = OctopusCloudDatabase.OctopusCloudDatabase()
        
#        formularFields.regionname = self.request.get('regionname')
#        formularFields.endpointURL = self.request.get('endpointURL')
#        formularFields.accessKey = self.request.get('accessKey')
#        formularFields.secretAccessKey = self.request.get('secretAccessKey')

        formularFields.put() 
    
    # responsible for the headline-text
    def buildHeadline(self):
#        headline = _('Credentials and Regions')
        headline = 'Credentials and Regions'
        return headline
    
    # responsible for the form-headline-text
    def buildFormText(self):
#        formText = _('Connect to a storage-service')
        formText = 'Connect to a storage-service'
        return formText
    
    # responsible for the page-content
    def buildContent(self):
        content = ''
        return content
    
    # responsible for the build of the loginTable, which shows the available, logged-in storage-services
    def buildLoginTable(self, credentialsAmount, results):
        # if there are existing credentials, show them in the login-table, else show nothing
        # build up the table step-by-step
        loginTable = ''
        loginTable = loginTable + '<table border="1" cellspacing="0" cellpadding="5">'
        loginTable = loginTable + '<tr>'
        loginTable = loginTable + '<th>&nbsp;</th>'
        loginTable = loginTable + '<th align="center">'
#        loginTable = loginTable + _('Storage Service')
        loginTable = loginTable + 'Storage Service'
        loginTable = loginTable + '</th>'
        loginTable = loginTable + '<th align="center">'
#        loginTable = loginTable + _('Endpoint URL')
        loginTable = loginTable + 'Endpoint URL'
        loginTable = loginTable + '</th>'
        loginTable = loginTable + '<th align="center">'
#        loginTable = loginTable + _('Access Key')
        loginTable = loginTable + 'Access Key'
        loginTable = loginTable + '</th>'
        loginTable = loginTable + '<th align="center">'
#        loginTable = loginTable + _('Name (Description)')
        loginTable = loginTable + 'Name (Description)'
        loginTable = loginTable + '</th>'
        loginTable = loginTable + '</tr>'
            
        if credentialsAmount:
            for result in results:
                loginTable = loginTable + '<tr>'
                loginTable = loginTable + '<td>'
                loginTable = loginTable + '<a href="/deletecredentials?regionname='
                loginTable = loginTable + str(result.regionname)
                loginTable = loginTable + '&amp;endpointURL='
                loginTable = loginTable + str(result.endpointURL)
                loginTable = loginTable + '&amp;accessKey='
                loginTable = loginTable + str(result.accessKey)
                loginTable = loginTable + '" title="erase credentials">'
#                loginTable = loginTable + '<img src="view/images/trash2.png" width="20" height="20" border="0" alt="erase credentials">'
                loginTable = loginTable + '<img src="view/images/OctopusIconsScratch/01_Scratch/trash_full.png" width="28" height="28" border="0" alt="erase credentials">'
                loginTable = loginTable + '</a>'
                loginTable = loginTable + '</td>'
                loginTable = loginTable + '<td align="center">'
                loginTable = loginTable + str(result.regionname)                 
                loginTable = loginTable + '</td>'
                loginTable = loginTable + '<td align="center">'
                loginTable = loginTable + str(result.endpointURL)
                loginTable = loginTable + '</td>'
                loginTable = loginTable + '<td align="center">'
                loginTable = loginTable + str(result.accessKey)
                loginTable = loginTable + '</td>'
                loginTable = loginTable + '<td align="center">'
                loginTable = loginTable + str(result.regionname)
                loginTable = loginTable + '</td>'
                loginTable = loginTable + '</tr>'
            
        # build the button 'delete all credentials'
        loginTable = loginTable + '</table>'
        loginTable = loginTable + '<p></p>'
        loginTable = loginTable + '<form action="/deleteallcredentials" method="post" accept-charset="utf-8">'
        loginTable = loginTable + '<a href="/deleteallcredentials'
        loginTable = loginTable + '" title="delete all credentials'
        loginTable = loginTable + '">'
        loginTable = loginTable + '<input type="submit" value="'
#        loginTable = loginTable + _('Delete all credentials')
        loginTable = loginTable + 'Delete all credentials'
        loginTable = loginTable + '">'
        loginTable = loginTable + '</a>'
        loginTable = loginTable + '</form>'
        return loginTable
        
    # responsible for the build of the loginFrom, which receive the input-data from the formula
    def buildLoginForm(self):
        # build the form for the login to a storage-service - the fields comes from the database        
        loginForm = ''
        loginForm = loginForm + '<form action="/setcredentials" method="post" accept-charset="utf-8">'
        loginForm = loginForm + '<table border="0" cellspacing="0" cellpadding="5">'
        loginForm = loginForm + '<tr>'
        loginForm = loginForm + '<td>'
        loginForm = loginForm + str(RegionForm())
        loginForm = loginForm + '</td>'
        loginForm = loginForm + '</tr>'        
        loginForm = loginForm + '</table>'
        loginForm = loginForm + '<p></p>'
        loginForm = loginForm + '<table>'
        loginForm = loginForm + '<tr>' 
        loginForm = loginForm + '<td align="right">' 
        loginForm = loginForm + '<input type="submit" value="'
#        loginForm = loginForm + _('Connect')
        loginForm = loginForm + 'Connect'
        loginForm = loginForm + '">'
        loginForm = loginForm + '</td>'
        loginForm = loginForm + '<td align="right">'                       
        loginForm = loginForm + '<input type="reset" value="'
#        loginForm = loginForm + _('Cancel')
        loginForm = loginForm + 'Cancel'
        loginForm = loginForm + '">'               
        loginForm = loginForm + '</td>'        
        loginForm = loginForm + '</tr>'        
        loginForm = loginForm + '</table>'
        loginForm = loginForm + '</form>'
        
        return loginForm

    def externalBuilder(self, pSelf, templateValues, credentials):
        self.get(pSelf, templateValues, credentials)

